using System.Collections.Generic;
using MbUnit.Framework;
using MVP.Demo.Core;
using MVP.Demo.Model;
using MVP.Demo.Presentation;
using MVP.Demo.View;
using Rhino.Mocks;
using Rhino.Mocks.Impl;
using Rhino.Mocks.Interfaces;

namespace MVP.Demo.Test
{
    [TestFixture]
    public class WhenViewingPlayerListPage
    {
        private IPlayerRepository playerRepository;
        private IPlayerListView view;
        private PlayerListPresenter presenter;
        private MockRepository mockery;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            view = mockery.DynamicMock<IPlayerListView>();
            playerRepository = mockery.DynamicMock<IPlayerRepository>();
            presenter = new PlayerListPresenter(playerRepository, view);
        }

        [TearDown]
        public void TearDown()
        {
            using (mockery.Playback())
            {
                presenter.InitializeView();
            }
        }

        [Test]
        public void ShouldBindToSavePlayerEventOnView()
        {
            using (mockery.Record())
            {
                view.SavePlayer += delegate {};
                LastCall.IgnoreArguments();
            }
        }

        [Test]
        public void ShouldGetTeamIDFromTheView()
        {
            using (mockery.Record())
            {
                Expect.Call(view.TeamID).Return(-1);
            }
        }

        [Test]
        public void ShouldGetTheListOfPlayersForTheTeamFromTheDataStore()
        {
            SetupResult.For(view.TeamID).Return(-1);

            using (mockery.Record())
            {
                Expect.Call(playerRepository.GetPlayersForTeam(-1)).Return(new List<PlayerDTO>());
            }
        }

        [Test]
        public void ShouldLoadListOfPlayersToTheView()
        {
            using (mockery.Record())
            {
                view.ListOfPlayers = new List<PlayerDTO>();
                LastCall.IgnoreArguments();
            }
        }
    }

    [TestFixture]
    public class WhenSavingANewPlayer
    {
        private IPlayerRepository playerRepository;
        private IPlayerListView view;
        private PlayerListPresenter presenter;
        private MockRepository mockery;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            view = mockery.DynamicMock<IPlayerListView>();
            playerRepository = mockery.DynamicMock<IPlayerRepository>();
            presenter = new PlayerListPresenter(playerRepository, view);
        }

        [TearDown]
        public void TearDown()
        {
            using (mockery.Playback())
            {
                presenter.InitializeView();

                EventRaiser eventRaiser = new EventRaiser((IMockedObject)view, "SavePlayer");
                eventRaiser.Raise(null, null);
            }
        }


        [Test]   
        public void ShouldGetFirstNameOfTheNewPlayerFromTheView()
        {
            using (mockery.Record())
            {
                Expect.Call(view.FirstName).Return("");
            }
        }

        [Test]
        public void ShouldGetLastNameAndTeamIDFromTheView()
        {
            using (mockery.Record())
            {
                Expect.Call(view.LastName).Return("");
                Expect.Call(view.TeamID).Return(-1);
            }
        }
    }

    [TestFixture]
    public class WhenDeletingPlayer
    {
        private IPlayerRepository playerRepository;
        private IPlayerListView view;
        private PlayerListPresenter presenter;
        private MockRepository mockery;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            view = mockery.DynamicMock<IPlayerListView>();
            playerRepository = mockery.DynamicMock<IPlayerRepository>();
            presenter = new PlayerListPresenter(playerRepository, view);
        }

        [TearDown]
        public void TearDown()
        {
            using (mockery.Playback())
            {
                presenter.DeletePlayer("");
            }
        }

        [Test]
        public void ShouldDeletePlayerWithGivenID()
        {
            using (mockery.Record())
            {
                playerRepository.DeletePlayer("");
            }
        }

        [Test]
        public void ShouldGetTeamIDFromView()
        {
            using (mockery.Record())
            {
                Expect.Call(view.TeamID).Return(1);
            }
        }

        [Test]
        public void ShouldGetListOfPlayerDTOsForTeam()
        {
            SetupResult.For(view.TeamID).Return(1);

            using (mockery.Record())
            {
                Expect.Call(playerRepository.GetPlayersForTeam(1)).Return(new List<PlayerDTO>());
            }
        }

        [Test]
        public void ShouldLoadListOfPlayersToView()
        {
            using (mockery.Record())
            {
                view.ListOfPlayers = new List<PlayerDTO>();
                LastCall.IgnoreArguments();
            }
        }
    }
}
